package haoyou.csnf.manager.module.pm.service.rulesliving;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import haoyou.csnf.manager.module.pm.controller.admin.rulesliving.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.rulesliving.RulesLivingDO;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;

import haoyou.csnf.manager.module.pm.dal.mysql.rulesliving.RulesLivingMapper;

import static haoyou.csnf.manager.framework.common.exception.util.ServiceExceptionUtil.exception;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.convertList;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.diffList;
import static haoyou.csnf.manager.module.pm.enums.ErrorCodeConstants.*;

/**
 * 生活补贴规则 Service 实现类
 *
 * @author 好有经验源码
 */
@Service
@Validated
public class RulesLivingServiceImpl implements RulesLivingService {

    @Resource
    private RulesLivingMapper rulesLivingMapper;

    @Override
    public Integer createRulesLiving(RulesLivingSaveReqVO createReqVO) {
        // 插入
        RulesLivingDO rulesLiving = BeanUtils.toBean(createReqVO, RulesLivingDO.class);
        rulesLivingMapper.insert(rulesLiving);
        // 返回
        return rulesLiving.getId();
    }

    @Override
    public void updateRulesLiving(RulesLivingSaveReqVO updateReqVO) {
        // 校验存在
        validateRulesLivingExists(updateReqVO.getId());
        // 更新
        RulesLivingDO updateObj = BeanUtils.toBean(updateReqVO, RulesLivingDO.class);
        rulesLivingMapper.updateById(updateObj);
    }

    @Override
    public void deleteRulesLiving(Integer id) {
        // 校验存在
        validateRulesLivingExists(id);
        // 删除
        rulesLivingMapper.deleteById(id);
    }

    @Override
        public void deleteRulesLivingListByIds(List<Integer> ids) {
        // 校验存在
        validateRulesLivingExists(ids);
        // 删除
        rulesLivingMapper.deleteByIds(ids);
        }

    private void validateRulesLivingExists(List<Integer> ids) {
        List<RulesLivingDO> list = rulesLivingMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RULES_LIVING_NOT_EXISTS);
        }
    }

    private void validateRulesLivingExists(Integer id) {
        if (rulesLivingMapper.selectById(id) == null) {
            throw exception(RULES_LIVING_NOT_EXISTS);
        }
    }

    @Override
    public RulesLivingDO getRulesLiving(Integer id) {
        return rulesLivingMapper.selectById(id);
    }

    @Override
    public PageResult<RulesLivingDO> getRulesLivingPage(RulesLivingPageReqVO pageReqVO) {
        return rulesLivingMapper.selectPage(pageReqVO);
    }

}